package org.lzuploader.gui.listeners.options;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.lzuploader.gui.WOptions;
import org.lzuploader.gui.helpers.EasyMessageBox;
import org.lzuploader.model.Localization;

/** This class is used when a user wants to browse his hard drive for World of Warcraft.
 * 
 * @author Emmanuel Cron
 *
 */
public class BrowseSelectionListener implements SelectionListener
{
    /** The logger for this class. */
    private static Log log = LogFactory.getLog(BrowseSelectionListener.class);
    
    /** Localization of this class. */
    private Localization localization = Localization.getInstance();
    
    /** Used when the widget is selected by default.
     * @param e The associated event
     */
    @Override
    public void widgetDefaultSelected(final SelectionEvent e)
    {
        widgetSelected(e);
    }

    /** Used when the user clicked on the widget.
     * @param e The associated event
     */
    @Override
    public void widgetSelected(final SelectionEvent e)
    {
        log.debug("Browsing for World of Warcraft.");
        
        log.trace("Building window with various options.");
        
        Shell wOptionsShell = null;
        for (int i = 0 ; i < e.display.getShells().length ; i++)
        {
            if (e.display.getShells()[i].getData() instanceof WOptions)
            {
                wOptionsShell = e.display.getShells()[i];
                break;
            }
        }
        
        final FileDialog fileDialog = new FileDialog(wOptionsShell, SWT.OPEN);
        
        final String[] extFilters = { "WoW.exe" };
        final String[] nameFilters = { "World of Warcraft" };
        
        fileDialog.setFilterExtensions(extFilters);
        fileDialog.setFilterNames(nameFilters);
        
        log.trace("Opening window.");
        
        final String path = fileDialog.open();
        
        if (path != null)
        {   
            log.trace("Selected path: " + path);
            
            final File file = new File(path);
            
            if (!file.exists())
            {
                EasyMessageBox.error(
                    localization.getString("DIALOG_TITLE_ERROR"), 
                    localization.getString("DIALOG_WOW_INVALID_PATH")
                );
            }
            else
            {
                log.trace("Removing WoW.exe from path.");
                
                final StringBuffer basedir = new StringBuffer(path);
                basedir.delete(basedir.length() - 7, basedir.length());
                
                log.trace("Setting wow path Text to selected path.");
                
                final WOptions options = (WOptions) wOptionsShell.getData();
                options.getWowPathWidget().setText(basedir.toString());
            }
        }
    }

}
